﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LanguageAnalyse;
using System.Xml;

namespace OISTools
{
    public partial class AnalyseApmodel : Form
    {
        DataAccess.SqlProvider db;

        public AnalyseApmodel()
        {
            InitializeComponent();
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            List<Apmodel> list1 = LoadModel(textBox1.Text);
            List<apmodelg> list2 = LoadModelg(textBox1.Text);


            var listColl = from l1 in list1
                           join l2 in list2 on l1.pgid equals l2.pgid
                           select new { pgid = l1.pgid, pname = l1.name, tbname = l2.tbname };



            string sqlInsertTemp = @"INSERT INTO [MyHome].[dbo].[apmodel]([pgid],[pname],[tbname])  VALUES('{0}','{1}','{2}');";

            StringBuilder sbTextColl = new StringBuilder();
            sbTextColl.Append("delete from apmodel;");
            foreach (var c in listColl.Distinct().ToList())
            {
                sbTextColl.Append(string.Format(sqlInsertTemp, c.pgid, c.pname, c.tbname));
            }

            db.ExecuteSQL(sbTextColl.ToString());
            MessageBox.Show("保存成功!");

        }

        private List<Apmodel> LoadModel(string xmlDataPath)
        {

            List<Apmodel> listModel = new List<Apmodel>();
            string filename = System.IO.Path.Combine(xmlDataPath, "apmodel.xml");

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filename);
            for (int i = 1; i < xmlDoc.ChildNodes[1].ChildNodes.Count; i++)
            {
                XmlNode row = xmlDoc.ChildNodes[1].ChildNodes[i];
                string pid = row.SelectSingleNode("pgid").InnerText;
                string name = row.SelectSingleNode("name").InnerText;
                //string gid = row.SelectSingleNode("gid").InnerText;
                //if(); 过滤多余项目。
                listModel.Add(new Apmodel { pgid = pid, name = name });
            }
            return listModel;
        }


        private List<apmodelg> LoadModelg(string xmlDataPath)
        {
            string filename = System.IO.Path.Combine(xmlDataPath, "apmodelg.xml");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filename);
            List<apmodelg> list1 = new List<apmodelg>();

            for(int i=1;i< xmlDoc.ChildNodes[1].ChildNodes.Count;i++)
            {
                XmlNode row = xmlDoc.ChildNodes[1].ChildNodes[i];
                apmodelg a = new apmodelg();
                a.pgid = row.SelectSingleNode("pgid").InnerText;
                a.tbname = row.SelectSingleNode("tbname").InnerText;

                list1.Add(a);
            }
            return list1;
        }



        private string getVallue(XmlNode node)
        {
            if (node != null)
                return node.InnerText;
            return "";
        }

        public class apmodelg
        {
            public string pgid{get;set;}
            public string tbname{get;set;}
        }

        private void AnalyseApmodel_Load(object sender, EventArgs e)
        {
            db = new DataAccess.SqlProvider(OisLibrary.DB.Connection);
        }
    }
}
